/**
 * 判断当前浏览器环境
 * @returns 是Chrome非Edge返回true，否则返回false
 */
export const isChromeNotEdge = () => {
    if (window.navigator.userAgent.indexOf('Chrome') > -1 && window.navigator.userAgent.indexOf('Edge') === -1) {
        return true;
    } else {
        return false;
    }
};
/**
 * 浏览器文件下载 chrome 遇到pdf 会直接预览不下载，注意像产品、测试解释
 * @param fileUrl 文件地址
 * @param fileName 文件名称
 */
export const downloadForBrowser = (fileUrl: string, fileName: string): void => {
    const eleLink = document.createElement('a');
    eleLink.download = fileName;
    eleLink['target'] = '_blank';
    eleLink.style.display = 'none';
    // 图片转base64地址
    eleLink.href = fileUrl;
    // 触发点击
    document.body.appendChild(eleLink);
    eleLink.click();
    // 然后移除
    document.body.removeChild(eleLink);
};

/**
 * 获取文件的base64编码
 * @param file Blob 或 File 对象
 * @returns 读取成功返回data:URL格式的字符串（base64编码）；读取失败返回错误信息
 */
export const getBase64 = (file: File | Blob | undefined): Promise<string> => {
    if (!file) return Promise.reject(new Error('no file'));
    return new Promise((resolve, reject) => {
        const reader = new FileReader();
        reader.readAsDataURL(file);
        reader.onload = () => resolve(reader.result as string);
        reader.onerror = (error) => reject(error);
    });
};

/**
 * 根据base64字符串封装blob对象
 * @param base64Str base64字符串
 * @param mimeTypeStr mimetype字符串
 * @returns blob对象
 */
export const dataURLToBlob = (base64Str: string, mimeTypeStr: string): Blob | undefined => {
    const bstr = window.atob(base64Str); // 解码 base-64 编码的字符串，base-64 编码使用方法是 btoa()
    let length = bstr.length;
    const u8arr = new Uint8Array(length); // 创建初始化为0的，包含length个元素的无符号整型数组
    while (length--) {
        u8arr[length] = bstr.charCodeAt(length); // 返回在指定的位置的字符的 Unicode 编码
    }
    return new Blob([u8arr], { type: mimeTypeStr }); // 返回一个blob对象
};

/**
 * 后端返回base64公共导出
 * @param base64Str base64字符串
 * @param mimeTypeStr mimetype字符串
 * @param fileName 文件名
 */
export const downloadFileByBase64 = (base64Str, mimeTypeStr, fileName) => {
    const myBlob = dataURLToBlob(base64Str, mimeTypeStr);
    const myUrl = window.URL.createObjectURL(myBlob);
    downloadForBrowser(myUrl, fileName);
};
/**
 * 后端返回文件流公共导出
 * @param blobData blob数据
 * @param mimeTypeStr mimetype字符串
 * @param fileName 文件名
 * @param callback 导出成功后的回调函数
 */
export const downloadFileByFileFlow = (blobData, mimeTypeStr, fileName, callback: () => void) => {
    const blob = new Blob([blobData], { type: mimeTypeStr });
    const hrefUrl = window.URL.createObjectURL(blob);
    downloadForBrowser(hrefUrl, fileName);
    callback();
};

export function showFullscreen(fullarea, isFullScreen = false) {
    if (isFullScreen) {
        const mydoc = document as any;
        // 退出全屏
        if (mydoc.exitFullscreen) {
            mydoc.exitFullscreen();
        } else if (mydoc.webkitCancelFullScreen) {
            mydoc.webkitCancelFullScreen();
        } else if (mydoc.mozCancelFullScreen) {
            mydoc.mozCancelFullScreen();
        } else if (mydoc.msExitFullscreen) {
            mydoc.msExitFullscreen();
        }
    } else {
        // 进入全屏
        if (fullarea.requestFullscreen) {
            fullarea.requestFullscreen();
        } else if (fullarea.webkitRequestFullScreen) {
            fullarea.webkitRequestFullScreen();
        } else if (fullarea.mozRequestFullScreen) {
            fullarea.mozRequestFullScreen();
        } else if (fullarea.msRequestFullscreen) {
            // IE11
            fullarea.msRequestFullscreen();
        }
    }
    return !isFullScreen;
}

export default {
    showFullscreen,
    isChromeNotEdge,
    downloadForBrowser,
    getBase64,
    dataURLToBlob,
    downloadFileByBase64,
    downloadFileByFileFlow,
};
